package com.quarkus.demo;

import com.demo.core.service.UserService;
import com.demo.core.aop.AopAnno;
import com.demo.core.entity.UserBody;
import com.demo.core.entity.UserQuery;
import com.demo.core.exception.BizException;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.UriInfo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.jboss.resteasy.reactive.RestResponse;

import java.net.URI;

@Path("/user")
@RequiredArgsConstructor
public class UserResource {

    private final UserService userService;


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object user(@QueryParam("name") String name, @QueryParam("gender") Integer gender) {
        return userService.user(name, gender);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/query")
    @AopAnno
    public Object query(@BeanParam UserQuery queryObj) {
        return queryObj;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/exception")
    public Object exception(@BeanParam UserQuery queryObj) {
        throw new BizException("哦豁");
    }


    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public void readBodyPut(UserQuery body){
        System.out.println(body);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void readBodyPost(UserBody body){
        System.out.println(body);
    }

    @GET
    @Path("/redirect")
    public Object redirect(@Context UriInfo uriInfo){

        return RestResponse.seeOther(uriInfo.getAbsolutePathBuilder().path("/query").build());
    }

    @GET
    @Path("/redirectBaidu")
    @SneakyThrows
    public Object redirectBaidu(@Context UriInfo uriInfo){

        return RestResponse.seeOther(new URI("https://www.baidu.com"));
    }
}
